
package ren.jinhuo.boots;

import com.baomidou.mybatisplus.generator.config.TemplateConfig;

import java.util.Arrays;

public class CodeGenerator {
    //"e:\\codeGen"
    static final String outPutDir="d:\\codeGen";

    @org.junit.Test
    public void test2() {
        System.out.println(Arrays.toString("1.2.3".split("\\.")));
        String packageName = "com.quhaizu.cloud.dingtalk";
        String[] tabPrefix = new String[]{"dt"};
        generateByTables("jdbc:mysql://cdb-xxx.com:3306/dingtalk","xxUser","xxPwd",packageName,tabPrefix
                ,"dt_refund_apply"
                ,                "dt_refund_approval"
                ,                "dt_sys_config"
                ,                "dt_user_relation"
        );
    }

    private void generateByTables(String jdbcUrl,String userName,String pwd, String packageName, String[] tabPrefix, String... tableNames) {


        // 代码生成器
        com.baomidou.mybatisplus.generator.AutoGenerator autoGenerator = new com.baomidou.mybatisplus.generator.AutoGenerator();

        // 全局配置
        com.baomidou.mybatisplus.generator.config.GlobalConfig globalConfig = new com.baomidou.mybatisplus.generator.config.GlobalConfig() ;

        // 数据源配置
        com.baomidou.mybatisplus.generator.config.DataSourceConfig dataSourceConfig = new  com.baomidou.mybatisplus.generator.config.DataSourceConfig();
        // 程序包配置
        com.baomidou.mybatisplus.generator.config.PackageConfig packageConfig = new com.baomidou.mybatisplus.generator.config.PackageConfig();
        // 名称策略配置
        com.baomidou.mybatisplus.generator.config.StrategyConfig strategyConfig = new com.baomidou.mybatisplus.generator.config.StrategyConfig();

        globalConfig.setActiveRecord(false);// 设置不需要活动对象,  @TableId 少了个else
        globalConfig.setAuthor("wangjx");
        globalConfig.setOutputDir(outPutDir);
        globalConfig.setFileOverride(true);
        globalConfig.setSwagger2(true);
        globalConfig.setEnableCache(false);
        globalConfig.setBaseColumnList(true);



        dataSourceConfig.setDbType(com.baomidou.mybatisplus.annotation.DbType.MYSQL);
        dataSourceConfig.setUrl(jdbcUrl);

        dataSourceConfig.setUsername(userName);
        dataSourceConfig.setPassword(pwd);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");

        packageConfig.setParent(packageName);
        packageConfig.setController("controller");
        packageConfig.setEntity("entity");
        packageConfig.setXml("mapper");

         //此处可以修改为您的表前缀；
         //nochange  underline_to_camel；strategyConfig.setFieldNaming(NamingStrategy.underline_to_camel);.setNaming(NamingStrategy.remove_prefix_and_camel);
        strategyConfig.setTablePrefix(tabPrefix);
        strategyConfig.setCapitalMode(false);
//        // strategyConfig.setDbColumnUnderline(true);
//        //
//        // strategyConfig.setColumnNaming(com.baomidou.mybatisplus.generator.config.rules.NamingStrategy.underline_to_camel);
        strategyConfig.setNaming(com.baomidou.mybatisplus.generator.config.rules.NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(com.baomidou.mybatisplus.generator.config.rules.NamingStrategy.nochange);// nochange;underline_to_camel
        strategyConfig.setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        strategyConfig.setEntityLombokModel(true);// @Data @Accessors(chain = true)

        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setPackageInfo(packageConfig);

        /*AbstractTemplateEngine*/
        TemplateConfig tc = new TemplateConfig();
        autoGenerator.setTemplate(tc);

        autoGenerator.execute();

    }



}